package com.powergisol.gis.service.impl;

import com.powergisol.gis.service.ProgressService;
import com.powergisol.gis.util.FileSuffixRename;
import com.powergisol.gis.util.RepalceFileContent;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.io.File;
import java.math.BigDecimal;

/**
 * @author Frog
 */
@Service
public class ProgressServiceImpl implements ProgressService {

    @Autowired
    private RedisTemplate redisTemplate;

    private static Logger logger = LoggerFactory.getLogger(ProgressServiceImpl.class);

    @Override
    public Double osgb2Pld(String uuid,String in, String out) {
        if ((in == null || in.equals("")) || (out == null || in.equals(""))) {
            return 0.0;
        }
        long countIn = 0;
        if(null==redisTemplate.opsForValue().get(uuid + "TotalExchange")){
            countIn = getlist(new File(in));
            redisTemplate.opsForValue().set(uuid + "TotalExchange",countIn);
        }else{
            countIn = Long.valueOf(redisTemplate.opsForValue().get(uuid + "TotalExchange").toString());
        }
        long countOut = getlist(new File(out));
        BigDecimal percentage = BigDecimal.ZERO;
        if(countIn>0){
            percentage = new BigDecimal(countOut).divide(new BigDecimal(countIn),2,BigDecimal.ROUND_DOWN);
        }
        //生成json文件则转换完成
        File fileJson = new File(out+File.separator+"model.json");
        if(fileJson.exists()){
            percentage = new BigDecimal("1");
            //转换为1则修改json文件和文件后缀名
            RepalceFileContent.replaceFileStr(out+File.separator +"model.json",".lob",".pld");
            FileSuffixRename.reName(out,"lob","pld");
            redisTemplate.delete(uuid + "TotalExchange");
        }
        logger.info("pld in="+countIn+" out="+countOut+" percentage="+percentage.toString());
        return Double.valueOf(percentage.toString());
    }

    @Override
    public Double osgb2b3dm(String uuid,String in, String out) {
        if ((in == null || in.equals("")) || (out == null || in.equals(""))) {
            return 0.0;
        }

        long countB3dmIn = 0;
        if(null==redisTemplate.opsForValue().get(uuid + "TotalExchange")){
            countB3dmIn = getlist(new File(in));
            redisTemplate.opsForValue().set(uuid + "TotalExchange",countB3dmIn);
        }else{
            countB3dmIn = Long.valueOf(redisTemplate.opsForValue().get(uuid + "TotalExchange").toString());
        }
        long countOut = getlist(new File(out));
        BigDecimal percentage = BigDecimal.ZERO;
        if(countB3dmIn>0){
            percentage = new BigDecimal(countOut).divide(new BigDecimal(countB3dmIn),2,BigDecimal.ROUND_DOWN);
        }
        //生成json文件则转换完成
        File fileJson = new File(out+File.separator+"tileset.json");
        if(fileJson.exists()){
            percentage = new BigDecimal("1");
            redisTemplate.delete(uuid + "TotalExchange");
        }
        logger.info("b3dm in="+countB3dmIn+" out="+countOut + " percentage="+percentage.toString());
        return Double.valueOf(percentage.toString());
    }

    @Override
    public Double gltfCompression(String uuid,String in, String out) {
        if ((in == null || in.equals("")) || (out == null || in.equals(""))) {
            return 0.0;
        }

        long countIn = getlist(new File(in));
        long countOut = getlist(new File(out));

        BigDecimal percentage = BigDecimal.ZERO;
        if(countIn>0){
            percentage = new BigDecimal(countOut).divide(new BigDecimal(countIn),2,BigDecimal.ROUND_DOWN);
        }

        if (countOut > countIn) {
            percentage = new BigDecimal("1");
        }
        return  Double.valueOf(percentage.toString());
    }


    @Override
    public Double objToGltf(String uuid,String in, String out) {
        if ((in == null || in.equals("")) || (out == null || in.equals(""))) {
            return 0.0;
        }
        File fileOut = new File(out);
        if(fileOut.exists()){
            return 1d;
        }else{
            return 0.0;
        }
    }

    @Override
    public Double tif2terrain(String uuid,String layers, String out) {
        File file = new File(out);
        File[] fileList = file.listFiles();
        int folderCount = getFolderCount(fileList);
        Double percentage = (double) Math.round((folderCount / Double.valueOf(layers)) * 10000) / 10000;

        if(percentage<0){
            percentage = 0.0;
        }

        if (percentage >= 1) {
            percentage = 1.0;
        }
        return percentage;
    }

    private static long getlist(File f) {// 递归求取目录文件个数
        long size = 0;
        File flist[] = f.listFiles();
        if(null == flist){
            return size;
        }
        size = flist.length;
        for (int i = 0; i < flist.length; i++) {
            if (flist[i].isDirectory()) {
                size = size + getlist(flist[i]);
                size--;
            }
        }
        return size;
    }

    private static int getFolderCount(File[] fileArr) {
        int count = 0;
        for (int i = 0; i < fileArr.length; i++) {
            //判断是否为文件夹
            if (fileArr[i].isDirectory()) {
                count++;
            }
        }
        return count - 1;
    }


}
